//
//  RSComposeToolBarStackView.swift
//  RSWeibo
//
//  Created by Macx on 16/8/17.
//  Copyright © 2016年 Macx. All rights reserved.
//

import UIKit

// 定义一个枚举, 用来标记五个按钮
enum RSComposeBarButtonType: Int {

    case picture = 0
    
    case mention = 1
    case trend = 2
    case emoticon = 3
    case add = 4
    }

class RSComposeToolBarStackView: UIStackView {
    
    // 定义一个全局变量的 按钮, 用来记录表情键盘按钮, 在别的方法了可以拿到这个按钮进行按钮背景图片的切换
    var emotionKeyboardButton: UIButton?
    
    // 点击按钮要执行的闭包
    var didselectedToolBarButtonClosure: ((type: RSComposeBarButtonType)->())?

    override  init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 添加布局控件的方法
    private func setupUI(){
        
//        backgroundColor = UIColor.grayColor()
        
        //  水平方向布局
        axis = .Horizontal
        //  等比填充
        distribution = .FillEqually

    
        addChildButton("compose_toolbar_picture", type: .picture)
        addChildButton("compose_mentionbutton_background", type: .mention)
        addChildButton("compose_trendbutton_background", type: .trend)
       emotionKeyboardButton = addChildButton("compose_emoticonbutton_background", type: .emoticon)
        addChildButton("compose_add_background", type: .add)
        
    }
    
    // 定义一个创建按钮的通用方法
    private func addChildButton(imageName: String, type: RSComposeBarButtonType) -> UIButton{
    
        let btn = UIButton()
        
        // 从枚举的原始值, 作为按钮的 tag 值
        btn.tag = type.rawValue
        
        btn.setImage(UIImage(named: imageName), forState: .Normal)
        btn.setImage(UIImage(named: "\(imageName)_highlighted"), forState: .Highlighted)
        
        // 给按钮添加点击事件
        btn.addTarget(self, action: "toolbarButtonAction:", forControlEvents: .TouchUpInside)
        
        // 添加
        addArrangedSubview(btn)
        
        return btn
        
    }
    
    // 按钮的点击事件
    @objc private func toolbarButtonAction(button: UIButton){
    
        // 根据传进来的按钮的tag值取到对应的type
        let type = RSComposeBarButtonType(rawValue: button.tag)
//        print(type)
        
        // 执行闭包, 传入参数
        didselectedToolBarButtonClosure?(type: type!)
        
    }
    
    // 切换表情按钮和系统键盘
    func swithToEmotionButton(isemotion: Bool){
    
        if isemotion {
            // 如果是表情键盘, 就把按键背景切换成系统键盘图标
            
            emotionKeyboardButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: .Normal)
            emotionKeyboardButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: .Highlighted)

        }else{
        
            // 如果是系统键盘图标, 就切换成表情
            //  显示表情键盘的icon
            emotionKeyboardButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: .Normal)
            emotionKeyboardButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: .Highlighted)
        }
        
    }

}
